package com.zzedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.zzedu.common.annotation.DataScope;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.common.utils.bean.BeanUtils;
import com.zzedu.domain.ZzSchool;
import com.zzedu.domain.ZzTeacher;
import com.zzedu.dto.ZzTeacherDTO;
import com.zzedu.mapper.ZzTeacherMapper;
import com.zzedu.service.IZzSchoolService;
import com.zzedu.service.IZzTeacherService;
import com.zzedu.system.service.ISysRegionService;
import com.zzedu.vo.ZzTeacherVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 教师管理Service业务层处理
 *
 * @author Mr.Wang
 * @date 2025-02-16
 */
@Service
public class ZzTeacherServiceImpl extends ServiceImpl<ZzTeacherMapper, ZzTeacher> implements IZzTeacherService {

    @Autowired
    ISysRegionService sysRegionService;

    @Autowired
    IZzSchoolService zzSchoolService;

    @Override
    public LambdaQueryWrapper<ZzTeacher> buildQuery(ZzTeacherDTO zzTeacher) {
        LambdaQueryWrapper<ZzTeacher> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(null != zzTeacher.getSchoolId(), ZzTeacher::getSchoolId, zzTeacher.getSchoolId());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzTeacher.getName()), ZzTeacher::getName, zzTeacher.getName());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzTeacher.getTel()), ZzTeacher::getTel, zzTeacher.getTel());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzTeacher.getProvince()), ZzTeacher::getProvince, zzTeacher.getProvince());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzTeacher.getCity()), ZzTeacher::getCity, zzTeacher.getCity());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzTeacher.getDistrict()), ZzTeacher::getDistrict, zzTeacher.getDistrict());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzTeacher.getAddress()), ZzTeacher::getAddress, zzTeacher.getAddress());
        lambdaQueryWrapper.eq(null != zzTeacher.getState(), ZzTeacher::getState, zzTeacher.getState());
        if (CollectionUtils.isNotEmpty(zzTeacher.getSchoolIdArray())) {
            lambdaQueryWrapper.in(ZzTeacher::getSchoolId, zzTeacher.getSchoolIdArray());
        }
        List<String[]> queryProvinceAndCityAndDistrict = zzTeacher.getQueryProvinceAndCityAndDistrict();
        if (CollectionUtils.isNotEmpty(queryProvinceAndCityAndDistrict)) {
            List<String> province = Lists.newArrayList();
            List<String> city = Lists.newArrayList();
            List<String> district = Lists.newArrayList();
            for (String[] provinceAndCityAndDistrict : queryProvinceAndCityAndDistrict) {
                if (null == provinceAndCityAndDistrict) {
                    continue;
                }
                if (StringUtils.isNotBlank(provinceAndCityAndDistrict[0])) {
                    province.add(provinceAndCityAndDistrict[0]);
                }
                if (provinceAndCityAndDistrict.length > 1 && StringUtils.isNotBlank(provinceAndCityAndDistrict[1])) {
                    city.add(provinceAndCityAndDistrict[1]);
                }
                if (provinceAndCityAndDistrict.length > 2 && StringUtils.isNotBlank(provinceAndCityAndDistrict[2])) {
                    district.add(provinceAndCityAndDistrict[2]);
                }
            }
            lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(province), ZzTeacher::getProvince, province);
            lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(city), ZzTeacher::getCity, city);
            lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(district), ZzTeacher::getDistrict, district);
        }
        lambdaQueryWrapper.orderByDesc(ZzTeacher::getId);
        return lambdaQueryWrapper;
    }


    @DataScope(otherIdColumnName = "school_id")
    public <E extends IPage<ZzTeacher>> E page(E page, Wrapper<ZzTeacher> queryWrapper) {
        return super.page(page, queryWrapper);
    }

    @Override
    @DataScope(otherIdColumnName = "school_id")
    public boolean updateById(ZzTeacher entity) {
        return super.updateById(entity);
    }

    public List<ZzTeacherVO> convertVO(List<ZzTeacher> zzTeacherList) {
        return Optional.ofNullable(zzTeacherList).orElse(Lists.newArrayList()).stream().map(zzTeacher -> {
            ZzTeacherVO zzTeacherVO = new ZzTeacherVO();
            BeanUtils.copyBeanProp(zzTeacherVO, zzTeacher);
            List<String> provinceAndCityAndDistrict = sysRegionService.provinceAndCityAndDistrict(Lists.newArrayList(zzTeacher.getProvince(), zzTeacher.getCity(), zzTeacher.getDistrict()));
            if (CollectionUtils.isNotEmpty(provinceAndCityAndDistrict)) {
                zzTeacherVO.setProvinceAndCityAndDistrict(Joiner.on("/").join(provinceAndCityAndDistrict));
            }
            ZzSchool zzSchool = zzSchoolService.getById(zzTeacher.getSchoolId());
            if (null != zzSchool) {
                zzTeacherVO.setSchoolName(zzSchool.getSchoolName());
            }
            return zzTeacherVO;
        }).collect(Collectors.toList());
    }
}